import { add@{modelName} } from '@/services/@{serviceName}Service';
import { ProColumns, ProTable } from '@ant-design/pro-components';
import { useModel } from '@umijs/max';
import { message, Modal } from 'antd';
import React, { PropsWithChildren } from 'react';


interface CreateModalProps
{
  modalVisible: boolean;
  columns: ProColumns<@{modelName}Type.@{modelName}>[];
  onSubmit: () => void;
  onCancel: () => void;
  typeList: any[];
}

/**
 * 
 * @param props
 * @constructor
 */
const CreateModal: React.FC<PropsWithChildren<CreateModalProps>> = (props) =>
{
  let { modalVisible, columns, onSubmit, onCancel } = props;
  const { initialState } = useModel('@@initialState');
  const loginUser = initialState?.loginUser;

  /**
 * 
 * @param fields
 */
const handleAdd = async (fields: @{modelName}Type.@{modelName}) =>
{
  const hide = message.loading('正在添加');
  try
  {
    fields.AddUserId = loginUser?.Id;
    fields.UpdateUserId = loginUser?.Id;
    await add@{modelName}({ ...fields } as @{modelName}Type.@{modelName});
    hide();
    message.success('添加成功');
    return true;
  } catch (error)
  {
    hide();
    message.error('添加失败请重试！');
    return false;
  }
};

  return (
    <Modal
      destroyOnClose
      title="新建"
      open={modalVisible}
      onCancel={() => onCancel()}
      footer={null}
    >
      <ProTable<@{modelName}Type.@{modelName}, @{modelName}Type.@{modelName}>
        onSubmit={async (value) =>
        {
          const success = await handleAdd(value);
          if (success)
          {
            onSubmit?.();
          }
        }}
        rowKey="id"
        type="form"
        columns={columns}
      />
    </Modal>
  );
};

export default CreateModal;
